from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf

mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

learning_rate = 0.01
batch_size = 128
n_step = 30

X = tf.placeholder(tf.float32, [batch_size, 784])
Y = tf.placeholder(tf.int32, [batch_size, 10])

with tf.name_scope("Wx_b") as scope:
    w = tf.Variable(tf.random_normal(shape=[784, 10], stddev=0.01), name="weights")
    b = tf.Variable(tf.zeros([1, 10]), name="bias")
    logits = tf.matmul(X, w) + b

with tf.name_scope("cost") as scope:
    entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y, name="loss")
    loss = tf.reduce_mean(entropy) #计算误差的平均值
    tf.summary.scalar("loss", loss)

with tf.name_scope("train") as scope:
    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

summary = tf.summary.merge_all()

with tf.Session() as sess:  # 在tensorboard上可视化
    writer = tf.summary.FileWriter('/tmp/graphs/logistic_reg', sess.graph)
    sess.run(tf.global_variables_initializer())
    n_batch = int(mnist.train.num_examples/batch_size)

    for i in range(n_step):   # 训练模型n_step次
        total_loss = 0
        for j in range(n_batch):
            X_batch, Y_batch = mnist.train.next_batch(batch_size)
            _, loss_batch = sess.run([optimizer, loss], feed_dict={X: X_batch, Y: Y_batch})
            total_loss += loss_batch
            summary_str = sess.run(summary, feed_dict={X: X_batch, Y: Y_batch})
            writer.add_summary(summary_str, i * n_batch)
            print("average loss : {0},{1}".format(i, total_loss/n_batch))

